<?php

App::uses('AppController', 'Controller');

/**
 * Staffbooks Controller
 *
 * @property Staffbook $Staffbook
 */
class StaffbooksController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Staffbook->recursive = 0;
        $this->paginate = array('order' => array('Staffbook.timestamp' => 'DESC'));
        $this->set('staffbooks', $this->paginate());

        $this->loadModel('UsersStaffbook');
        $usersStaffbooks = $this->UsersStaffbook->find('all', array('conditions' => array('user_id' => $this->Session->read('Auth.User.user_id'))));
        $this->set(compact('usersStaffbooks'));
        
        
        
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Staffbook->exists($id)) {
            throw new NotFoundException('Invalid staffbook', 'default', array('class' => 'alert alert-danger'));
        }
        $options = array('conditions' => array('Staffbook.' . $this->Staffbook->primaryKey => $id));
        $this->set('staffbook', $this->Staffbook->find('first', $options));

        //$staffBookArr['Staffbook']['isRead'] = true;
//                $this->Staffbook->id = $id;
//                $this->Staffbook->saveField('isRead', true );

        $this->loadModel('UsersStaffbook');

        $usersStaffbook = $this->UsersStaffbook->find('first', array('conditions' => array('staffbook_id' => $id, 'user_id' => $this->Session->read('Auth.User.user_id'))));

        if (empty($usersStaffbook)) {
            $this->UsersStaffbook->set('staffbook_id', $id);
            $this->UsersStaffbook->set('user_id', $this->Session->read('Auth.User.user_id'));
            $this->UsersStaffbook->saveAll();
        }
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Staffbook->create();
            if ($this->Staffbook->save($this->request->data)) {
                $this->Staffbook->saveField('timestamp', date("Y-m-d H:i:s"));
                $this->Session->setFlash('The new Staff book entry has been saved', 'default', array('class' => 'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The new Staff book could not be saved. Please, try again.', 'default', array('class' => 'alert alert-danger'));
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->Staffbook->exists($id)) {
            throw new NotFoundException('Invalid staffbook', 'default', array('class' => 'alert alert-danger'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Staffbook->save($this->request->data)) {
                $this->Staffbook->saveField('timestamp', date("Y-m-d H:i:s"));
                $this->Session->setFlash('The Staff book entry has been successfully edited', 'default', array('class' => 'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The Staff book could not be edited. Please, try again.', 'default', array('class' => 'alert alert-danger'));
            }
        } else {
            $options = array('conditions' => array('Staffbook.' . $this->Staffbook->primaryKey => $id));
            $this->request->data = $this->Staffbook->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Staffbook->id = $id;
        if (!$this->Staffbook->exists()) {
            throw new NotFoundException('Invalid staffbook', 'default', array('class' => 'alert alert-danger'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Staffbook->delete()) {
            $this->Session->setFlash('Staff book deleted', 'default', array('class' => 'alert alert-info'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash('Staff book was not deleted', 'default', array('class' => 'alert alert-danger'));
        $this->redirect(array('action' => 'index'));
    }

    public function beforeFilter() {
        parent::beforeFilter();
        //$this->Auth->allow('index', 'view');
    }

}

